﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using HF.Business.Sale;
using HF.Framework.Web;
using HF.Business.Common;

public partial class CustomerEdit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            initData();
    }
    private void initData()
    {
        if (Request["Customer_Code"] != null)
        {
            btnAdd.Visible = false;
            Customers c = new Customers();
            c.GetCustomersInfo(Request["Customer_Code"].ToString());
            tbxCustCode.Text = c.Customer_Code;
            tbxCustCaption.Text = c.Customer_Caption;
            tbxCustClassId.Text = c.Customer_ClassId;
            tbxCustClassName.Text = CustomerClass.GetClassName(c.Customer_ClassId);
            tbxCustCorporation.Text = c.Customer_Corporation;
            tbxCustBank.Text = c.Customer_Bank;
            tbxCustBankAccount.Text = c.Customer_BankAccount;
            tbxCustTaxAccount.Text = c.Customer_TaxAccount;
            tbxCustAddress.Text = c.Customer_Address;
            tbxCustHome.Text = c.Customer_HomePage;
            cbxUsed.Checked = c.Customer_Used;
            tbxCustPost.Text = c.Customer_Post;
            tbxCustTel.Text = c.Customer_Tel;
            tbxCustFax.Text = c.Customer_Fax;
            tbxCustEmail.Text = c.Customer_Email;
            tbxCustLinkMan.Text = c.Customer_Linker;
            tbxCustRebate.Text = c.Customer_Rebate.ToString();
            drpCustStatus.SelectedIndex = c.Customer_Status;
            tbxCustDes.Text = c.Customer_Memo;
            lbCreateUserId.Text = c.CreateUserId;
        }
        else
        {
            btnModify.Visible = false;
            tbxCustCode.Text = BusinessComm.GetDeciCode("CUSTOMER", 6);
 
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (Customers.CustomerExists(tbxCustCode.Text))
        {
            WorkDialog.ShowMessage(this,tbxCustCode.Text+"该编号的客户已存在,编号不能重复!!");
            return;
        }
        if (tbxCustCode.Text.Length <= 0)
        {
            WorkDialog.ShowMessage(this, "编号不能为空!!");
            return;
        }
        try
        {
            Customers c = new Customers();
            c.Customer_Code = tbxCustCode.Text;
            c.Customer_Caption = tbxCustCaption.Text;
            c.Customer_ClassId = tbxCustClassId.Text;
            c.Customer_Corporation = tbxCustCorporation.Text;
            c.Customer_Bank = tbxCustBank.Text;
            c.Customer_BankAccount = tbxCustBankAccount.Text;
            c.Customer_TaxAccount = tbxCustTaxAccount.Text;
            c.Customer_Address = tbxCustAddress.Text;
            c.Customer_HomePage = tbxCustHome.Text;
            c.Customer_Used = cbxUsed.Checked;
            c.Customer_Post = tbxCustPost.Text;
            c.Customer_Tel = tbxCustTel.Text;
            c.Customer_Fax = tbxCustFax.Text;
            c.Customer_Email = tbxCustEmail.Text;
            c.Customer_Linker = tbxCustLinkMan.Text;
            c.Customer_Rebate = Convert.ToDouble(tbxCustRebate.Text);
            c.Customer_Status = drpCustStatus.SelectedIndex;
            c.Customer_Memo = tbxCustDes.Text;
            c.CreateUserId = User.Identity.Name.ToString();
            c.Insert();
            WorkDialog.ShowMessage(this, "增加成功!");
        }
        catch (Exception ex)
        {
            WorkDialog.ShowMessage(this, "增加错误:" + ex.Message.ToString());
        }
    }
    
    protected void btnModify_Click(object sender, EventArgs e)
    {
        if (tbxCustCode.Text.Length <= 0)
        {
            WorkDialog.ShowMessage(this, "编号不能为空!!");
            return;
        }
        try
        {
            Customers c = new Customers();

            c.Customer_Code = tbxCustCode.Text;
            c.Customer_Caption = tbxCustCaption.Text;
            c.Customer_ClassId = tbxCustClassId.Text;

            c.Customer_Corporation = tbxCustCorporation.Text;
            c.Customer_Bank = tbxCustBank.Text;
            c.Customer_BankAccount = tbxCustBankAccount.Text;
            c.Customer_TaxAccount = tbxCustTaxAccount.Text;
            c.Customer_Address = tbxCustAddress.Text;
            c.Customer_HomePage = tbxCustHome.Text;
            c.Customer_Used = cbxUsed.Checked;
            c.Customer_Post = tbxCustPost.Text;
            c.Customer_Tel = tbxCustTel.Text;
            c.Customer_Fax = tbxCustFax.Text;
            c.Customer_Email = tbxCustEmail.Text;
            c.Customer_Linker = tbxCustLinkMan.Text;
            c.Customer_Rebate = Convert.ToDouble(tbxCustRebate.Text);
            c.Customer_Status = drpCustStatus.SelectedIndex;
            c.Customer_Memo = tbxCustDes.Text;
            c.CreateUserId = lbCreateUserId.Text;
            c.Update();
            WorkDialog.ShowMessage(this, "修改成功!");
        }
        catch (Exception ex)
        {
            WorkDialog.ShowMessage(this, "修改错误:" + ex.Message.ToString());
        }
    }
    
}
